<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>非受控组件</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
</head>
<body>
    <div id="app"></div>
    <script type="text/babel">
        class FormComponent extends React.Component{
            // 创建容器，装载原生 DOM 元素
            nameNodeContainer = new React.createRef()
            passwordNodeContainer = new React.createRef()
            render(){
                return (
                    <form onSubmit={this.submitCallback}>
                        用户名：<input type="text" name='username' ref={this.nameNodeContainer}/><br/> {/*通过 ref 将对应原生 DOM 放到容器中*/}
                        密码： <input type="password" name='username' ref={this.passwordNodeContainer}/><br/>
                        <button>提交</button>
                    </form>
                )
            }

            submitCallback = (event) => {
                event.preventDefault();
                const {current: usernameNode} = this.nameNodeContainer
                const {current: passwordNode} = this.passwordNodeContainer
                //直接获取 DOM 元素，并进行操作
                alert(`用户名：${usernameNode.value}，密码：${passwordNode.value}`)
            }
        }
        ReactDOM.render(<FormComponent/>, document.querySelector('#app'))
    </script>
</body>
</html>